package com.lumivi.util;

//import org.sqlite.SQLite;

import processing.core.PApplet;

import com.lumivi.devices.XIMMachine;
import com.lumivi.devices.XIMTracking;
import com.lumivi.game.XIMApplication;

import de.bezier.data.sql.SQLite;

public class MovementRecorder extends XIMApplication
{
	SQLite db;

	int pX, pY; 			// previous x and y vars
	int currentSubject;		// subject being recorded
	
	// database structure
	// subject: id
	// experiments: id, id_subject, numTile, timestamp
	// scores: id, score_w, score_wo
	
	public MovementRecorder(PApplet p, XIMMachine m)
	{
		super(p, m);

		try
		{
			db = new SQLite(p, "test.db"); // open database file
			db.connect();
		} catch (Exception e)
		{
			// TODO: handle exception
		}

	}

	public void create()
	{
		db
				.query("CREATE TABLE MOVEMENTS (timestamp datetime default CURRENT_TIMESTAMP, id_subject int, id_movement int, pos_x int, pos_y int)");
		db
				.query("CREATE TABLE SUBJECTS (id_subject int, num_tile int, timestamp datetime default CURRENT_TIMESTAMP)");
		db
				.query("CREATE TABLE SCORES (id_subject int, id_experiment int, score_w, score_wo, timestamp datetime default CURRENT_TIMESTAMP)");

	}

	public void insertSubject()
	{

	}

	@Override
	public void execute()
	{
		XIMTracking t = this.getTracking();

		if (t.getTileX() != pX || t.getTileY() != pY)
		{
			this.updateCoords();
			this.record();
		}

	}

	private void record()
	{
		
	}

	private void updateCoords()
	{
		this.pX = this.getTracking().getTileX();
		this.pY = this.getTracking().getTileY();
	}
	
	public void keyPressed(char key)
	{
		if (key == 'r')
			;
		if (key == 's')
			;
	}
	
}
